package com.example.shop_2212.search

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.base.entity.Record
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.shop_2212.R
import com.example.shop_2212.adapter.GoodsAdapter
import com.example.shop_2212.adapter.RecordAdapter
import com.example.shop_2212.databinding.ActivitySearchBinding
import com.example.shop_2212.room.AppDBUtils
import com.google.android.material.chip.Chip

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsAdapter
    lateinit var recordAdapter: RecordAdapter
    override fun initView() {
        //初始化
        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(this,2)

  /*      recordAdapter = RecordAdapter()
        binding.rvRecord.adapter = recordAdapter
        binding.rvRecord.layoutManager = GridLayoutManager(this,4)*/
        //搜索控件使用
        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车执行
                if (query != null) {
                    viewModel.search(query,10,1)
                    //保存成功
                    AppDBUtils.getDB().getRecordDao().insert(Record(query,SPUtils.getInstance().getString("phone")))
                    queryRecord()//实时刷新历史记录
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文本发生变化执行
               /* if (newText != null) {
                    viewModel.search(newText,10,1)
                }*/
                return false
            }
        })
        queryRecord()
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/XqActivity").withInt("gid",goodsAdapter.data[position].id).navigation()
        }

    }

    private fun queryRecord() {
        //查询数据库
        val records =
            AppDBUtils.getDB().getRecordDao().query(SPUtils.getInstance().getString("phone"))
        //展示数据
     /*   recordAdapter.data.clear()
        recordAdapter.data.addAll(records)
        recordAdapter.notifyDataSetChanged()*/
        if(records.isNotEmpty()){
            binding.rvRecord.removeAllViews()//清空之前的记录
            records.forEach {
                val chip = LayoutInflater.from(this).inflate(R.layout.item_record, null) as Chip
                chip.findViewById<TextView>(R.id.record_tv).text = it.keyword
                binding.rvRecord.addView(chip)
                //点击再次查询
                chip.setOnClickListener {it1->
                    binding.sv.setQuery(it.keyword,true)
                }
                //点击关闭图标，删除数据
                chip.setOnCloseIconClickListener {it2->
                    //页面上删除
                    binding.rvRecord.removeView(it2)
                    //后台数据也删除
                    AppDBUtils.getDB().getRecordDao().delete(it)
                }
            }
        }
    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if(it.code== Const.RESULT_OK){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> = SearchViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_search

}